package com.xpjhs.resources.web.controller;

import javax.servlet.http.HttpServletRequest;






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xpjhs.common.util.DateFormatUtil;
import com.xpjhs.resources.model.Resources;
import com.xpjhs.resources.service.ResourcesService;
import com.xpjhs.user.model.User;

/**
 * 所有关于表单的操作以及需要查找单个的操作都放到这里
 * 
 * @author guohuijun
 * @createDate 2014年7月18日 上午11:36:32 @ QQ: 263235040
 */
@Controller
@RequestMapping("/resources")
public class ResourcesFormController {
	@Autowired
	private ResourcesService reService;

	@ModelAttribute("resources")
	public Resources getResources(@PathVariable long id) {
		Resources resources = id == 0 ? new Resources() : reService.findOne(id);
		return resources;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param resources
	 * @return
	 */
	@RequestMapping(value = "/junior/delete/{id}")
	public String delete(@PathVariable long id,
			@ModelAttribute("resources") Resources resources,
			RedirectAttributes redirectAttributes) {
		reService.delete(resources);
		redirectAttributes.addFlashAttribute("message", "数据删除成功！！！");
		return "redirect:/resources/junior";
	}

	/**
	 * 跳转编辑UI
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable long id, ModelMap map) {

		return null;
	}

	/**
	 * 添加and保存
	 * 
	 * @param id
	 * @param resources
	 * @return
	 */

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String save(@PathVariable long id,
			@ModelAttribute("resources") Resources resources,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		resources.setChecker("luckyboy");
		User user = (User) request.getSession().getAttribute("user");
		resources.setCreater(user.getRealName());
		resources.setStatus("0");
		resources.setCreateTime(DateFormatUtil.getDateFormat());
		resources.setModyfiTime(DateFormatUtil.getDateFormat());
		reService.save(resources);
		redirectAttributes.addFlashAttribute("message", "数据添加成功！！！");
		return "redirect:resources/junior";
	}

}
